
const app = getApp()
Page({
    data: {
        pageNum:1,
        pageTotal:0,
        mall_list: [],
        mall: {}
    },
    onLoad: function (options) {
        console.log(options)
        this.setData({
            typeId:options.id
        })
        this.getGoodsList()
        this.getGoodsDetail(options.id)
    },
    onShareAppMessage() {
        return {
            title: '梦与玩',
            query: `/pages/market/market-categoty-list/market-categoty-list?id=${this.data.typeId}`
        }
    },
    onShareTimeline() {
        return {
            title: '梦与玩',
            query: `/pages/market/market-categoty-list/market-categoty-list?id=${this.data.typeId}`
        }
    },
    onReachBottom() {
        let _this = this;
        _this.setData({
            pageNum: _this.data.pageNum+1
        })
        wx.showLoading({
          title: '加载更多...',
          icon: 'loading'
        })
        _this.getGoodsList()
    },
    getGoodsList() {
        let _this = this;
        app.util.request({
            url: "api/mall/findByTypeId",
            method: "post",
            data: {
                typeId: _this.data.typeId,
                page: _this.data.pageNum,
                pageSize: 10
            },
            success(response) {
                wx.hideLoading()
                if (response.data.code == 0) {
                    let arr = response.data.data.list;
                    for(let i=0; i<arr.length; i++) {
                       arr[i].checked = _this.judgePreProduct(arr[i])
                    }
                    _this.setData({
                        pageTotal:response.data.data.pages,
                        mall_list: [..._this.data.mall_list,...arr]
                    })
                }
            }
        })
    },
    getGoodsDetail(typeId) {
        let _this = this;
        app.util.request({
            url: "api/type/find",
            method: "post",
            data: {
                id: typeId
            },
            success(response) {
                if (response.data.code == 0) {
                    _this.setData({
                        mall: response.data.data
                    })
                }
            }
        })
    },
    toPersonalGoods(e) {
        wx.navigateTo({
            url: '/pages/goods/goods-detail/goods-detail?id=' + e.currentTarget.dataset.id,
        })
    },
    // 判断是否是预售商品
    judgePreProduct(item) {
        let dB = new Date(item.sellTime.replace(/-/g, '/')); // 预售时间
        let d = new Date();
        let str = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate(); // 当前时间
        if (Date.parse(str) < Date.parse(dB)) {
            return true
        } else {
            return false
        }
    }
})
